<template>
  <div class="app-container">
    <el-card class="search-card" shadow="never">
      <!-- 搜索操作 -->
      <template #header>
        <div class="card-header">
          <span>筛选条件</span>
          <el-button
            class="collapse-btn"
            type="text"
            @click="toggleSearch"
            :icon="showSearch ? 'ArrowDownBold' : 'ArrowUpBold'"
          >
            {{ showSearch ? "收起" : "展开" }}
          </el-button>
        </div>
      </template>

      <!-- 查询区域 -->
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="88px"
      >
        <el-form-item label="订单ID" prop="orderId">
          <el-input
            v-model="queryParams.orderId"
            placeholder="请输入关联订单ID"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderCode">
          <el-input
            v-model="queryParams.orderCode"
            placeholder="请输入订单编号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="商品ID" prop="goodsId">
          <el-input
            v-model="queryParams.goodsId"
            placeholder="请输入商品ID"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="规格ID" prop="skuId">
          <el-input
            v-model="queryParams.skuId"
            placeholder="请输入规格ID"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <el-form-item label="规格名称" prop="skuName">
          <el-input
            v-model="queryParams.skuName"
            placeholder="请输入规格名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="核销码" prop="verificationCode">
          <el-input
            v-model="queryParams.verificationCode"
            placeholder="请输入独立核销码"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="核销码哈希值" prop="verificationHash">
          <el-input
            v-model="queryParams.verificationHash"
            placeholder="请输入核销码哈希值"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <el-form-item label="使用状态" prop="useStatus">
          <el-select
            v-model="queryParams.useStatus"
            placeholder="请选择使用状态"
            clearable
          >
            <el-option
              v-for="dict in use_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="核销时间" prop="useTime">
          <el-date-picker
            clearable
            v-model="queryParams.useTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择核销时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="核销操作人" prop="useBy">
          <el-input
            v-model="queryParams.useBy"
            placeholder="请输入核销操作人"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="赠送状态" prop="giftedStatus">
          <el-select
            v-model="queryParams.giftedStatus"
            placeholder="请选择赠送状态"
            clearable
          >
            <el-option
              v-for="dict in gifted_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="赠送人" prop="giverId">
          <el-input
            v-model="queryParams.giverId"
            placeholder="请输入赠送人ID"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="被赠人用户" prop="receiverId">
          <el-input
            v-model="queryParams.receiverId"
            placeholder="请输入被赠人用户ID"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="赠送时间" prop="giftTime">
          <el-date-picker
            clearable
            v-model="queryParams.giftTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择赠送时间"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="有效期开始时间" prop="validStartTime">
          <el-date-picker
            clearable
            v-model="queryParams.validStartTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择有效期开始时间"
          >
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="有效期结束时间" prop="validEndTime">
          <el-date-picker
            clearable
            v-model="queryParams.validEndTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择有效期结束时间"
          >
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="版本号" prop="version">
          <el-input
            v-model="queryParams.version"
            placeholder="请输入版本号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ruoyi:verificationDetail:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyi:verificationDetail:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyi:verificationDetail:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ruoyi:verificationDetail:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表单数据区域 -->
    <el-table
      v-loading="loading"
      :data="verificationDetailList"
      border
      resizable
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" min-width="60" align="center" prop="detailId" />
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="订单编号" min-width="180" align="center" prop="orderCode" />
      <!-- <el-table-column label="商品ID" align="center" prop="goodsId" /> -->
      <!-- <el-table-column label="规格ID" align="center" prop="skuId" /> -->
      <el-table-column label="规格名称" align="center" prop="skuName" />
      <el-table-column
        label="核销码"
        align="center"
        prop="verificationCode"
      />
      <!-- <el-table-column
        label="核销码哈希值"
        align="center"
        prop="verificationHash"
      /> -->
      <el-table-column label="使用状态" align="center" prop="useStatus">
        <template #default="scope">
          <dict-tag :options="use_status" :value="scope.row.useStatus" />
        </template>
      </el-table-column>
      <el-table-column
        label="核销时间"
        align="center"
        prop="useTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.useTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核销人" min-width="120" align="center" prop="useBy" />
      <el-table-column label="赠送状态" align="center" prop="giftedStatus">
        <template #default="scope">
          <dict-tag :options="gifted_status" :value="scope.row.giftedStatus" />
        </template>
      </el-table-column>
      <el-table-column label="赠送人" align="center" prop="giverId" />
      <el-table-column label="被赠用户" align="center" prop="receiverId" />
      <el-table-column
        label="赠送时间"
        align="center"
        prop="giftTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.giftTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="有效期开始时间"
        align="center"
        prop="validStartTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.validStartTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="有效期结束时间"
        align="center"
        prop="validEndTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.validEndTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="版本号" align="center" prop="version" /> -->
      <el-table-column label="备注信息" min-width="120" align="center" prop="remark" show-overflow-tooltip="true" />
      <el-table-column
        label="操作"
        min-width="150"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi:verificationDetail:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi:verificationDetail:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改核销码明细信息对话框 -->
    <el-dialog :title="title" v-model="open" width="720px" append-to-body>
      <el-form
        ref="verificationDetailRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-collapse v-model="activeNames">
          <!-- 基础信息分组 -->
          <el-collapse-item name="baseInfo" title="基础信息">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="订单ID" prop="orderId">
                  <el-input
                    v-model="form.orderId"
                    placeholder="请输入关联订单ID"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="订单编号" prop="orderCode">
                  <el-input
                    v-model="form.orderCode"
                    placeholder="请输入订单编号"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="商品ID" prop="goodsId">
                  <el-input v-model="form.goodsId" placeholder="请输入商品ID" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="规格ID" prop="skuId">
                  <el-input v-model="form.skuId" placeholder="请输入规格ID" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="规格名称" prop="skuName">
                  <el-input
                    v-model="form.skuName"
                    placeholder="请输入规格名称"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="核销码" prop="verificationCode">
                  <el-input
                    v-model="form.verificationCode"
                    placeholder="请输入独立核销码"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="24">
                <!-- <el-form-item label="核销码哈希值" prop="verificationHash">
                  <el-input
                    v-model="form.verificationHash"
                    placeholder="请输入核销码哈希值"
                  />
                </el-form-item> -->
              </el-col>
            </el-row>
          </el-collapse-item>

          <!-- 核销信息分组 -->
          <el-collapse-item name="verificationInfo" title="核销信息">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="使用状态" prop="useStatus">
                  <el-select
                    v-model="form.useStatus"
                    placeholder="请选择使用状态"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="dict in use_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="核销时间" prop="useTime">
                  <el-date-picker
                    v-model="form.useTime"
                    type="date"
                    value-format="YYYY-MM-DD"
                    placeholder="请选择核销时间"
                    style="width: 100%"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="核销人" prop="useBy">
                  <el-input
                    v-model="form.useBy"
                    placeholder="请输入核销操作人"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>

          <!-- 赠送信息分组 -->
          <el-collapse-item name="giftInfo" title="赠送信息">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="赠送状态" prop="giftedStatus">
                  <el-select
                    v-model="form.giftedStatus"
                    placeholder="请选择赠送状态"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="dict in gifted_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="赠送人" prop="giverId">
                  <el-input
                    v-model="form.giverId"
                    placeholder="请输入赠送人ID"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="被赠用户" prop="receiverId">
                  <el-input
                    v-model="form.receiverId"
                    placeholder="请输入被赠人用户ID"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="赠送时间" prop="giftTime">
                  <el-date-picker
                    v-model="form.giftTime"
                    type="date"
                    value-format="YYYY-MM-DD"
                    placeholder="请选择赠送时间"
                    style="width: 100%"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>

          <!-- 有效期分组 -->
          <el-collapse-item name="validPeriod" title="有效期">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="有效期开始时间" prop="validStartTime">
                  <el-date-picker
                    v-model="form.validStartTime"
                    type="date"
                    value-format="YYYY-MM-DD"
                    placeholder="请选择有效期开始时间"
                    style="width: 100%"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="有效期结束时间" prop="validEndTime">
                  <el-date-picker
                    v-model="form.validEndTime"
                    type="date"
                    value-format="YYYY-MM-DD"
                    placeholder="请选择有效期结束时间"
                    style="width: 100%"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>

          <!-- 其他信息分组 -->
          <el-collapse-item name="otherInfo" title="其他信息">
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="删除" prop="delFlag">
                  <el-radio-group v-model="form.delFlag">
                    <el-radio
                      v-for="dict in del_flag"
                      :key="dict.value"
                      :label="dict.value"
                      >{{ dict.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <!-- <el-form-item label="版本号" prop="version">
                  <el-input v-model="form.version" placeholder="请输入版本号" />
                </el-form-item> -->
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注信息" prop="remark">
                  <el-input
                    v-model="form.remark"
                    type="textarea"
                    placeholder="请输入内容"
                    rows="3"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
        </el-collapse>

        <el-divider />
        
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup name="VerificationDetail">
import {
  listVerificationDetail,
  getVerificationDetail,
  delVerificationDetail,
  addVerificationDetail,
  updateVerificationDetail,
} from "@/api/ruoyi/verificationDetail";
import { parseTime } from '@/utils/ruoyi';

const { proxy } = getCurrentInstance();
const { use_status, gifted_status, del_flag } = proxy.useDict(
  "use_status",
  "gifted_status",
  "del_flag"
);

const verificationDetailList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const activeNames = ref(["baseInfo", "verificationInfo"]); // 默认展开基础信息和核销信息

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    orderCode: null,
    goodsId: null,
    skuId: null,
    skuName: null,
    verificationCode: null,
    verificationHash: null,
    useStatus: null,
    useTime: null,
    useBy: null,
    giftedStatus: null,
    giverId: null,
    receiverId: null,
    giftTime: null,
    validStartTime: null,
    validEndTime: null,
    version: null,
  },
  rules: {
    orderId: [
      { required: true, message: "关联订单ID不能为空", trigger: "blur" },
    ],
    orderCode: [
      { required: true, message: "订单编号不能为空", trigger: "blur" },
    ],
    goodsId: [{ required: true, message: "商品ID不能为空", trigger: "blur" }],
    skuId: [{ required: true, message: "规格ID不能为空", trigger: "blur" }],
    skuName: [{ required: true, message: "规格名称不能为空", trigger: "blur" }],
    useStatus: [
      { required: true, message: "使用状态不能为空", trigger: "change" },
    ],
    giftedStatus: [
      { required: true, message: "赠送状态不能为空", trigger: "change" },
    ],
    delFlag: [
      { required: true, message: "删除标志不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

// 新增：搜索区域折叠/展开
function toggleSearch() {
  showSearch.value = !showSearch.value;
}

/** 查询核销码明细信息列表 */
function getList() {
  loading.value = true;
  listVerificationDetail(queryParams.value).then((response) => {
    // verificationDetailList.value = response.rows;
    verificationDetailList.value = response.rows.filter((item) => item.delFlag !== "2");
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    detailId: null,
    orderId: null,
    orderCode: null,
    goodsId: null,
    skuId: null,
    skuName: null,
    verificationCode: null,
    verificationHash: null,
    useStatus: null,
    useTime: null,
    useBy: null,
    giftedStatus: null,
    giverId: null,
    receiverId: null,
    giftTime: null,
    validStartTime: null,
    validEndTime: null,
    delFlag: null,
    version: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
  };
  proxy.resetForm("verificationDetailRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.detailId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加核销码明细信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _detailId = row.detailId || ids.value;
  getVerificationDetail(_detailId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改核销码明细信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["verificationDetailRef"].validate((valid) => {
    if (valid) {
      if (form.value.detailId != null) {
        updateVerificationDetail(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addVerificationDetail(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _detailIds = row.detailId || ids.value;
  proxy.$modal
    .confirm('是否确认删除核销码明细信息编号为"' + _detailIds + '"的数据项？')
    .then(function () {
      return delVerificationDetail(_detailIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi/verificationDetail/export",
    {
      ...queryParams.value,
    },
    `verificationDetail_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>

<style scoped>
/* 搜索卡片样式 */
.search-card {
  /* margin-bottom: 16px; */
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
  color: #303133;
}

.collapse-btn {
  padding: 0;
  font-size: 14px;
  color: #606266;
}
</style>

